package com.wxdc.nmgyt.nmgytwydc.utils;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.nfc.Tag;
import android.provider.Settings;
import android.support.design.widget.TabLayout;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;

import com.tbruyelle.rxpermissions2.RxPermissions;

/**
 * Created by Administrator on 2018/4/24.
 * 设备工具类
 *
 */

public class DeviceTool {
    private final String TAG = this.getClass().getSimpleName();
    /**
     * IMEI （唯一标识序列号）
     * <p>需与{@link #isPhone(Context)}一起使用</p>
     * <p>需添加权限 {@code <uses-permission android:name="android.permission.READ_PHONE_STATE"/>}</p>
     *
     * @param context 上下文
     * @return IMEI
     */
    public static String getIMEI(Context context) {
        String deviceId;
        if (isPhone(context)) {
            deviceId = getDeviceIdIMEI(context);
        } else {
            deviceId = getAndroidId(context);
        }
        return deviceId;
    }

    /**
     * 判断设备是否是手机
     *
     * @param context 上下文
     * @return {@code true}: 是<br>{@code false}: 否
     */
    public static boolean isPhone(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context
                .TELEPHONY_SERVICE);
        return tm.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE;
    }

    /**
     * 获取设备的IMEI
     *
     * @param context
     * @return
     */
    @SuppressLint("MissingPermission")
    public static String getDeviceIdIMEI(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context
                .TELEPHONY_SERVICE);

        Log.i("DeviceTool", "getDeviceIdIMEI:------------- "+tm.getDeviceId());
        return tm.getDeviceId();
    }
    /**
     * 获取ANDROID ID
     *
     * @param context
     * @return
     */
    public static String getAndroidId(Context context) {
        return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
    }

    /**
     * 获取显示屏宽度 DisplayMetrics.widthPixels和高度DisplayMetrics.heightPixels
     * @param manager
     * @return
     */
    private static DisplayMetrics outMetrics;
    public static DisplayMetrics getDisplayWindowWidth(WindowManager manager){
        if (outMetrics == null) {
            outMetrics = new DisplayMetrics();
        }
        if (manager != null) {
            manager.getDefaultDisplay().getMetrics(outMetrics);
        }
        return outMetrics;
    }
}
